package com.gateway.plugin.springcloud.handler;

import com.gateway.common.dto.ApiDefinition;
import com.gateway.common.enums.RpcTypeEnum;
import com.gateway.plugin.base.cache.ApiDataCache;
import com.gateway.plugin.base.handler.ApiDataHandler;
import com.gateway.plugin.base.handler.event.HandlerRefreshEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;

/**
 * @program: api-gateway
 * @description:
 * @author: YuKai Fan
 * @create: 2025/2/6 15:32
 **/
public class SpringCloudApiDataHandler implements ApiDataHandler, ApplicationEventPublisherAware {

    private ApplicationEventPublisher publisher;

    @Override
    public void handle(ApiDefinition apiDefinition) {
        ApiDataCache.getInstance().cache(apiDefinition);
    }

    @Override
    public void remove(ApiDefinition apiDefinition) {
        ApiDataCache.getInstance().remove(apiDefinition);
    }

    @Override
    public void refresh() {
        this.publisher.publishEvent(new HandlerRefreshEvent(this));
    }

    @Override
    public String rpcType() {
        return RpcTypeEnum.SPRING_CLOUD.getName();
    }

    @Override
    public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
        this.publisher = applicationEventPublisher;
    }
}
